package J20250227_dfs;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:小朋友崇拜圈  https://www.lanqiao.cn/problems/182/learning/?page=1&first_category_id=1&tags=DFS&tag_relation=intersection
 * User: 王圆豪
 * Date: 2025-03-12
 * Time: 23:14
 */
public class Main4 {
    static int[] arr;
    static boolean[] vis;
    static int tmp=0,max=0;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        arr = new int[n+1];
        for(int i = 1; i <= n; i++){
            arr[i] = scan.nextInt();
        }
        vis = new boolean[n+1];
        for(int i = 1; i <= n; i++){
            dfs(i,i);
            Arrays.fill(vis, false);
        }
        System.out.println(max);
        scan.close();
    }

    public static void dfs(int pos, int target){
        tmp++;
        if(arr[pos] == target){
            max = Math.max(tmp, max);
            tmp = 0;
            return;
        }

        if(!vis[pos] && arr[pos] != target){
            vis[pos] = true;
            dfs(arr[pos], target);
        }else{
            tmp = 0;
            return;
        }
    }
}
